<!DOCTYPE HTML>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  <link rel="shortcut icon" type="image/ico" href="favicon.ico" />
  <title>SlickGrid example 9: Row reordering</title>
  <link rel="stylesheet" href="../slick.grid.css" type="text/css"/>
  <link rel="stylesheet" href="../css/smoothness/jquery-ui.css" type="text/css"/>
  <link rel="stylesheet" href="examples.css" type="text/css"/>
  <style>
    .slickgrid-cell-reorder {
      cursor: move;
      background: url("../images/drag-handle.png") no-repeat center center;
    }

  </style>
</head>
<body>
<div style="position:relative">
  <div style="width:600px;">
    <div class="grid-header" style="width:100%">
      <label>Santa's TODO list:</label>
    </div>
    <div id="myGrid" style="width:100%;height:500px;"></div>
  </div>
  <div class="options-panel">
    <b>Tips:</b>
    <hr/>
    <div style="padding:6px;">
      Click to select, Ctrl-click to toggle selection, Shift-click to select a range.<br/>
      Drag one or more rows by the handle to reorder.
      <br/>
      <br/>
        <h2>View Source:</h2>
        <ul>
            <li><A href="https://github.com/6pac/SlickGrid/blob/master/examples/example9-row-reordering-simple.html" target="_sourcewindow"> View the source for this example on Github</a></li>
        </ul>
    </div>
  </div>
</div>

<script src="../lib/firebugx.js"></script>

<script src="../lib/jquery-1.12.4.min.js"></script>
<script src="../lib/jquery-ui.min.js"></script>
<script src="../lib/jquery.event.drag-2.3.0.js"></script>
<script src="../lib/jquery.event.drop-2.3.0.js"></script>

<script src="../slick.core.js"></script>
<script src="../plugins/slick.cellrangeselector.js"></script>
<script src="../plugins/slick.cellselectionmodel.js"></script>
<script src="../plugins/slick.rowselectionmodel.js"></script>
<script src="../plugins/slick.rowmovemanager.js"></script>
<script src="../slick.formatters.js"></script>
<script src="../slick.editors.js"></script>
<script src="../slick.grid.js"></script>

<script>
var grid;
var data = [];
var columns = [
  {
    id: "#",
    name: "",
    width: 40,
    behavior: "selectAndMove",
    selectable: false,
    resizable: false,
    cssClass: "slickgrid-cell-reorder"
  },
  {
    id: "name",
    name: "Name",
    field: "name",
    width: 500,
    cssClass: "cell-title",
    editor: Slick.Editors.Text
  },
  {
    id: "complete",
    name: "Complete",
    width: 60,
    cssClass: "cell-effort-driven",
    field: "complete",
    cannotTriggerInsert: true,
    formatter: Slick.Formatters.Checkmark,
    editor: Slick.Editors.Checkbox
  }
];

var options = {
  editable: true,
  enableAddRow: true,
  enableCellNavigation: true,
  forceFitColumns: true,
  autoEdit: false
};

$(function () {
  data = [
    { name: "Make a list", complete: true},
    { name: "Check it twice", complete: false},
    { name: "Find out who's naughty", complete: false},
    { name: "Find out who's nice", complete: false}
  ];
  console.log(data);
  
  grid = new Slick.Grid("#myGrid", data, columns, options);

  grid.setSelectionModel(new Slick.RowSelectionModel());

  var moveRowsPlugin = new Slick.RowMoveManager({
    cancelEditOnDrag: true
  });

  moveRowsPlugin.onBeforeMoveRows.subscribe(function (e, data) {
    for (var i = 0; i < data.rows.length; i++) {
      // no point in moving before or after itself
      if (data.rows[i] == data.insertBefore || data.rows[i] == data.insertBefore - 1) {
        e.stopPropagation();
        console.log('false   ' + getEventDataAsString(data));
        return false;
      }
    }
    console.log('true    ' + getEventDataAsString(data));
    return true;
  });

  function getEventDataAsString(data) {
    var s = "";
    s += "insertBefore: " + data.insertBefore;
    s += ", rows: ["
    for (var i=0; i<data.rows.length; i++) { s += (i===0?"":",") + data.rows[i]; }
    s += "]"
    return s;
  }
  
  moveRowsPlugin.onMoveRows.subscribe(function (e, args) {
    var extractedRows = [], left, right;
    var rows = args.rows;
    var insertBefore = args.insertBefore;
    left = data.slice(0, insertBefore);
    right = data.slice(insertBefore, data.length);

    rows.sort(function(a,b) { return a-b; });

    for (var i = 0; i < rows.length; i++) {
      extractedRows.push(data[rows[i]]);
    }

    rows.reverse();

    for (var i = 0; i < rows.length; i++) {
      var row = rows[i];
      if (row < insertBefore) {
        left.splice(row, 1);
      } else {
        right.splice(row - insertBefore, 1);
      }
    }

    data = left.concat(extractedRows.concat(right));

    var selectedRows = [];
    for (var i = 0; i < rows.length; i++)
      selectedRows.push(left.length + i);

    grid.resetActiveCell();
    grid.setData(data);
    grid.setSelectedRows(selectedRows);
    grid.render();
  });

  grid.registerPlugin(moveRowsPlugin);


  grid.onAddNewRow.subscribe(function (e, args) {
    var item = {name: "New task", complete: false};
    $.extend(item, args.item);
    data.push(item);
    grid.invalidateRows([data.length - 1]);
    grid.updateRowCount();
    grid.render();
  });
})
</script>
</body>
</html>
